package com.kb.erp.util;

import com.xxl.job.core.context.XxlJobHelper;
import jcifs.smb.*;

import java.io.*;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;

/**
 * @Auther:chenzx
 * @Date:2024-04-15
 * @Description:共享文件夹操作
 */
public class CopyShareFileUtil {

    /**
     * Description: 从本地上传文件到共享目录
     * @param remoteUrl 共享文件目录
     * @param localFilePath 本地文件绝对路径
     */
    public static void smbPut(String remoteUrl, String localFilePath, String userName, String pwd) {
        InputStream in = null;
        OutputStream out = null;
        try {
            File localFile = new File(localFilePath);

            String fileName = localFile.getName();
            //这里推荐使用这种方式进行用户名密码的校验，在url中拼接，如果需要特殊字符可能无法转换
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, userName, pwd);
            SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName, auth);
            in = new BufferedInputStream(new FileInputStream(localFile));
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
            byte[] buffer = new byte[1024];
            while(in.read(buffer)!=-1){
                out.write(buffer);
                buffer = new byte[1024];
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void copyToWindowsShare(String localFilePath, String remoteUrl, String userName, String password) {
        try {
            // 获取本地文件路径
            Path localPath = Paths.get(localFilePath);

            // 获取文件名
            String fileName = localPath.getFileName().toString();

            // 构建目标文件路径
            String remoteFilePath = remoteUrl + "/" + fileName;

            // 使用 NtlmPasswordAuthentication 进行身份验证
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, userName, password);

            // 构建目标文件的 SmbFile 对象
            SmbFile remoteFile = new SmbFile(remoteFilePath, auth);

            // 创建输出流
            try (OutputStream out = new SmbFileOutputStream(remoteFile)) {
                // 使用 Files.copy 方法进行文件拷贝
                Files.copy(localPath, out);
            }
        } catch (IOException e) {
            XxlJobHelper.log(LocalDateTime.now() + "-->设计数据共享到文件夹失败-->" + e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * Description: 从共享目录拷贝文件到本地
     * @param remoteUrl 共享目录上的文件路径
     * @param localDir 本地目录
     */
    public void smbGet(String remoteUrl,String localDir) {
        InputStream in = null;
        OutputStream out = null;
        try {
            SmbFile remoteFile = new SmbFile(remoteUrl);
            String fileName = remoteFile.getName();
            File localFile = new File(localDir+File.separator+fileName);
            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
            out = new BufferedOutputStream(new FileOutputStream(localFile));
            byte[] buffer = new byte[1024];
            while(in.read(buffer)!=-1){
                out.write(buffer);
                buffer = new byte[1024];
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 删除文件
     * @param remoteUrl 共享文件目录
     * @param fileName 要删除的文件名
     */
    public static void deleteFile(String remoteUrl, String fileName, String userName, String pwd) {
        SmbFile SmbFile;
        try {
            // smb://userName:passWord@host/path/shareFolderPath/fileName
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, userName, pwd);
            SmbFile = new SmbFile(remoteUrl + "/" + fileName, auth);
            if (SmbFile.exists()) {
                SmbFile.delete();
            }
        } catch (MalformedURLException | SmbException e) {
            e.printStackTrace();
        }
    }

}


